<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/4/7
 * Time: 14:17
 */

namespace app\controller\api;

use app\model\{
    Material,
    Product,
    Quotation,
    User AS model,
    Message,
    News,
    ProcessingFee AS pf,
    MaterialPrice AS mp,
    MaterialQuality AS mq
};
use think\facade\Db;
class Index extends Base
{
    public function login(){
        $mobile = input('mobile');
        $password = input('password');
        if (!empty($password)){
            $userinfo = model::where('username',$mobile)->find();
            if (empty($userinfo->id))
                return $this->fail('用户不存在');
            if ($userinfo->password != $password)
                return $this->fail('密码错误');
            $userinfo = cache($mobile,$userinfo,864000);
        }else{
            $userinfo = cache($mobile);
            if (empty($userinfo) || empty($userinfo->id))
                return $this->fail('登录错误:请输入账号密码');
        }
        return $this->msg($userinfo);
    }

    public function unlogin(){

    }

    public function register(){
        $mobile = input('mobile');
        $password = input('password');
        $corporate_name = input('corporate_name');
        $count = model::where('username',$mobile)->count();
        if ($count > 0)
            return $this->fail('号码已经被注册');
        $count = model::where('corporate_name',$corporate_name)->count();
        if ($count > 0)
            return $this->fail('公司名称已经被注册');
        model::create([
            'username' => $mobile,
            'password' => $password,
            'corporate_name' => $corporate_name
        ]);
        return $this->msg();
    }

    public function message(){
        $id = input('id');
        if (!empty($id)){
            $news = News::find($id);
            $message = Message::find($id);
        }else {
            $news = News::order('create_time DESC')->limit(10)->select();
            $message = Message::order('create_time DESC')->limit(3)->select();
        }
        return $this->msg([
            'news' => $news,
            'message' => $message
        ]);
    }

    public function user(){
        $mobile = input('username');
        if (empty($mobile)) return $this->fail();
        $userinfo = cache($mobile);
        if (!empty($userinfo)){
            $post = input('post.');
            $info = model::find($userinfo->id);
            $info->save($post);
            $userinfo = $info->find($info->id);
        }
        return $this->msg($userinfo);
    }

    public function product(){
        $id = input('id');
        $type = input('type');
        $post = input('post.');
        if($type == 'save') {
            $post['user_id'] = $this->userInfo->id;
            if (empty($id)) {
                unset($post['id']);
                Product::create($post);
            }else{
                $info = Product::find($id);
                $info->save($post);
            }
            return $this->msg();
        }else{
            if (empty($id)) {
                $post = input('post.');
                $where = [];
                //$where[] = ['user_id','=',$this->userInfo->id];
                $list = Product::where($where)
                    ->with(['user'])
                    ->where('material','IN',function($sql){
                        $sql->name('material_all')->field('name')->select();
                    })
                    ->where('material_attr','IN',function($sql){
                        $sql->name('material_all')->field('attr')->select();
                    })
                    ->order('create_time DESC')
                    ->paginate(['page' => $this->page, 'list_rows' => $this->size]);
                return $this->msg($list);
            } else {
                $info = Product::with(['user'])->find($id);
                return $this->msg($info);
            }
        }
    }

    public function cost(){
        $post = input('post.');
        if (!empty($post['artificial'])){
            cache($this->userInfo->id.'cost',$post);
        }
        $data = cache($this->userInfo->id.'cost');
        return $this->msg($data);
    }

    public function model_list(){
        $model = input('model','');
        return $this->msg(Db::name('model')->where('model','like','%'.$model.'%')->select());
    }

    public function material($uniqid = '',$type = '')
    {
        $list = new Material();
        if ($type == 'save') {
            $post = input('post.');
            unset($post['username']);
            $post['user_id'] = $this->userInfo->id;
            if (!empty($post['material']))unset($post['material']);
            if (!empty($post['material_attr']))unset($post['material_attr']);
            if (!empty($uniqid)){
                $post['create_time'] = date('Y-m-d');
                $list->is_new(true)->where('uniqid',$uniqid)->save($post);
                unset($post['uniqid']);
                unset($post['create_time']);
                $list->is_new(false)->save($post);
            }else{
                $post['uniqid'] = md5(time().rand(1,999999));
                $post['create_time'] = date('Y-m-d');
                $list->is_new(true)->save($post);
                unset($post['uniqid']);
                unset($post['create_time']);
                Material::create($post);
            }
            return $this->msg();
        }else{
            if (!empty($uniqid)) {
                $info = $list->is_new(true)->find($uniqid);
                return $this->msg($info);
            } else {
                $where[] = ['user_id','=',$this->userInfo->id];
                $list = $list->is_new(true)
                    ->where($where)
                    ->paginate(['page' => $this->page, 'list_rows' => $this->size]);
                return $this->msg($list);
            }
        }
    }

    public function material_list(){
        $type = input('type');
        $name = input('name','');
        $attr = input('attr','');
        $list = Db::name('material')
            ->where('attr','like','%'.$attr.'%')
            ->where('name','like','%'.$name.'%');
        if (empty($type))
            $list = $list->field('name')->group('name');
        else
            $list = $list->field('attr');
        $list = $list->select();
        return $this->msg($list);
    }

    public function blank($uniqid = '',$type = '')
    {
        $list = new Quotation();
        if ($type == 'save') {
            $post = input('post.');
            unset($post['username']);
            $post['user_id'] = $this->userInfo->id;
            if (!empty($uniqid)) {
                $post['create_time'] = date('Y-m-d');
                $list->is_new(true)->where('uniqid', $uniqid)->save($post);
                unset($post['uniqid']);
                unset($post['create_time']);
                $list->is_new(false)->save($post);
            } else {
                $post['uniqid'] = md5(time() . rand(1, 999999));
                $post['create_time'] = date('Y-m-d');
                $list->is_new(true)->save($post);
                unset($post['uniqid']);
                unset($post['create_time']);
                Material::create($post);
            }
            return $this->msg();
        }elseif ($type == 'search'){
            $post = input('post.');
            $where = [];
            $field = input('group');
            !empty($post['name']) && $where[] = ['name','like','%'.$post['name'].'%'];
            !empty($post['attr']) && $where[] = ['attr','like','%'.$post['attr'].'%'];
            !empty($post['model']) && $where[] = ['model','like','%'.$post['model'].'%'];
            $list = Db::name('quotation')
                ->field($field)
                ->where($where)
                ->group($field)
                ->limit(30)
                ->select();
            return $this->msg($list);
        }else{
            if (!empty($uniqid)) {
                $info = $list->is_new(true)->find($uniqid);
                return $this->msg($info);
            } else {
                $where[] = ['user_id','=',$this->userInfo->id];
                $list = $list->is_new(true)
                    ->where($where)
                    ->paginate(['page' => $this->page, 'list_rows' => $this->size]);
                return $this->msg($list);
            }
        }
    }
    public function calculator_all(){
        $pf = pf::select()->toArray();
        $mp = mp::select()->toArray();
        $mq = mq::with(['pf','mp'])->select()->toArray();
        return $this->msg([
            'mq' => $mq,
            'material_name' => array_column($mq,'name'),
            'material_value' => array_column($mq,'density'),
            'processing_fee_name' => array_column($pf,'name'),
            'processing_fee_value' => array_column($pf,'price'),
            'material_name_price_name' => array_column($mp,'name'),
            'material_name_price_value' =>array_column($mp,'price') ,
        ]);
    }

    public function price_image(){
        $supplier = input('supplier');
        $image = cache($supplier);
        return $this->msg(['image' => $image]);
    }
}